<?php

namespace App\Http\Controllers\Frontend\UserManage;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Frontend\Demand\DemandController;
use App\Http\Controllers\Frontend\Service\ServiceController;
use App\Http\Repositories\Demand\DemandRepo;
use App\Http\Repositories\Relation\UserFavoriteRepo;
use App\Http\Repositories\Relation\UserObjectRepo;
use App\Http\Repositories\Service\ServiceRepo;
use App\Http\Repositories\UserManage\UserRepo;
use App\Http\Requests\Validators\Backend\UserManage\UserValidator;
use App\Traits\HelpFunctions;
use Illuminate\Http\Request;

/**
 * 用户相关
 */
class UserController extends Controller
{
    use HelpFunctions;

    const VALIDATOR = UserValidator::class;

    public function __construct()
    {
        $this->self_repo = new UserRepo;
    }

    public function register(Request $request)
    {
        //参数验证
        $args = $request->only(
            'name',
            'gender',
            'card_id',
            'password',
        );

        $this->my_validate($args, 'storeRules');

        //微信小程序获取openid

        //微信小程序获取phone

        $open_id = '';
        $phone = '';
        $password = 'ad_repository' . rand(0, 10000);

        $args = array_merge([
            'phone' => $phone,
            'open_id' => $open_id,
            'password' => $password,
        ], $args);

        $user = $this->self_repo->createOne($args);

        $token = $this->login(["phone" => $args["phone"], "password" => $args["password"]]);

        return $this->returnResponse($token);
    }

    public function edit(Request $request, $id)
    {
        //参数验证
        $args = $request->only(
            'name',
            'gender',
            'card_id',
            'password',
        );
        $this->my_validate($args, 'editRules');
        if ($this->self_repo->uniqueExist($request->only('phone'), $id)) $this->clientError('该手机号已经存在');

        //根据id修改数据
        return $this->returnResponse($this->self_repo->editById($id, $args));
    }

    /**
     * 实名认证
     */
    public function realAuth(Request $request)
    {
        //参数验证
        $args = $request->only(
            'name',
            'card_id',
            'resources',
        );
        $this->my_validate($args, 'realAuthRules');

        return $this->returnResponse($this->self_repo->realAuth($args, $this->getUserId()));
    }

    /**
     * 用户收藏
     */
    public function favorite(Request $request)
    {
        //参数验证
        $args = $request->only(
            'object_id',
            'type',
        );
        $this->my_validate($args, 'favoriteRules');

        app(UserObjectRepo::class)->check($args);

        $args["user_id"] = $this->getUserId();

        if (app(UserFavoriteRepo::class)->favoriteOrNot($args)) {
            return $this->returnResponse([], 200, "收藏成功");
        } else {
            return $this->returnResponse([], 200, "取消收藏成功");
        }
    }

    public function favoriteIndex(Request $request)
    {
        //参数验证
        $args = $request->only(
            'type',
        );
        $this->my_validate($args, 'favoriteIndexRules');
        $args["user_id"] = $this->getUserId();

        return $this->returnResponse(app(UserFavoriteRepo::class)->search($args, $args["type"]));
    }

    public function objectShow(Request $request, $id)
    {
        $args = $request->only("type");
        $this->my_validate($args, 'objectShow');

        if ($args["type"] == config("project.user_object_relation.service")) {
            return app(ServiceController::class)->userShow($id);
        } elseif ($args["type"] == config("project.user_object_relation.demand")) {
            return app(DemandController::class)->userShow($id);
        }
    }

    public function objectIndex(Request $request)
    {
        $args = $request->only("type", "page", "page_num");
        $this->my_validate($args, 'objectIndex');

        $args["user_id"] = $this->getUserId();

        if ($args["type"] == config("project.user_object_relation.service")) {
            return app(ServiceRepo::class)->search($args);
        } else {
            return app(DemandRepo::class)->search($args);
        }
    }
}
